var Writable = require('stream').Writable;
var util = require('util');

module.exports = CountStream;

util.inherits(CountStream,Writable);

function CountStream(matchText, options){
  Writable.call(this, options);
  this.count = 0;
  this.matcher = new RegExp(matchText, 'g');
}

CountStream.prototype._write = function(chunk, encoding, cb){
  var matches = chunk.toString().match(this.matcher);
  if(matches){
    this.count += matches.length;
  }
  cb();
}
 
CountStream.prototype.end = function(){
  this.emit('total', this.count);
}